﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Threading;
using System.Collections.ObjectModel;
using SQLServerHealthCheck.Model;
using SQLServerOptimizer.PL.WPF;
using SQLServerHealthCheck.SQLServer;
using System.Data.SqlClient;
using AG.Utilities;

namespace SQLServerOptimizer.PL.WPF
{
    public delegate bool RunStatusCheck(SQLServerInstance sqlServerInstance);

    public delegate bool Connectioncheck(String Instance,String Status);

    public class InstanceStatusWorker : AsyncUtils.AsyncOperation
    {
        public event Connectioncheck StatusUpdate;
        public static List<Exception> errors;

        private ObservableCollection<SQLServerInstance> sqlServerInstances;
        public InstanceStatusWorker(DispatcherObject target, ObservableCollection<SQLServerInstance> SQLServerInstances)
            : base(target)
        {
            sqlServerInstances = SQLServerInstances;
        }


        protected override void DoWork()
        {
            foreach (SQLServerInstance sqlServerInstance in sqlServerInstances)
            {
                RunStatusCheck runStatusCheck = new RunStatusCheck(CheckInstanceState);
                runStatusCheck.BeginInvoke(sqlServerInstance, null, null);
              
            }
        }

        private bool CheckInstanceState(SQLServerInstance instance)
        {
            List<Exception> errors = new List<Exception>();
           
            bool online =false;
            try
            {
                online = CollectServerAndServiceData.IsSQLServerAccessible(instance.GetConnectionString("Master"));
                if (online == true)
                {
                    FireStatusUpdateEvent(instance.InstanceName,"Online");
                }
                else
                {
                    

                    FireStatusUpdateEvent(instance.InstanceName, "Offline");
                }
                
            }
            catch (SqlException sqlex)
            {

                errors.Add(sqlex);

            }
            catch (Exception exx)
            {

                errors.Add(exx);

            }
            return online;
        }
        public bool CheckDatabaseExists(string server, string database)
        {

            string connString = "Data Source=" + server + ";Initial Catalog=master;Integrated Security=True;";

            string cmdText = "select * from master.dbo.sysdatabases where name=\'" + database + "\'";

            bool bRet = false;
            try
            {
                using (SqlConnection sqlConnection = new SqlConnection(connString))
                {


                    sqlConnection.Open();

                    using (SqlCommand sqlCmd = new SqlCommand(cmdText, sqlConnection))
                    {

                        SqlDataReader reader = sqlCmd.ExecuteReader();

                        bRet = reader.HasRows;

                        reader.Close();

                    }

                    sqlConnection.Close();

                }


            }
            catch (Exception ex)
            {
                
                Logger.Error(ex);
            }

            return bRet;
        }
        private void FireStatusUpdateEvent(String Instance,String Status)
        {
            Object[] param = new Object[] { Instance, Status };

            FireAsync(StatusUpdate, param);
        
        
        }
    
    
    
    }
    

}
